package ar.uade.fain.ai.tpo.view.pedidos;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import ar.uade.fain.ai.tpo.Sistema;
import ar.uade.fain.ai.tpo.dto.PedidoDTO;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class AltaPedido extends javax.swing.JFrame {
	private JLabel idClienteLabel;
	private JTextField idClienteTextField;
	private JButton agregarItemPedidoButton;
	private JButton aceptarButton;
	private DefaultTableModel itemPedidoModel;
	private JScrollPane scroll;
	private JTable itemPedido;
	private List<Long> idcomidas;
	private List<Integer> cantComidas;
	private JButton obtenerPrecioButton;
	private JTextField precioTextField;
	private JLabel precioLabel;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AltaPedido inst = new AltaPedido();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public AltaPedido() {
		super();
		initGUI();
		this.idcomidas = new ArrayList<Long>();
		this.cantComidas = new ArrayList<Integer>();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				idClienteLabel = new JLabel();
				idClienteLabel.setText("idCliente:");
			}
			{
				precioLabel = new JLabel();
				precioLabel.setText("Precio:");
			}
			{
				precioTextField = new JTextField();
				precioTextField.setEditable(false);
			}
			{
				obtenerPrecioButton = new JButton();
				obtenerPrecioButton.setText("Ver Precio");
				obtenerPrecioButton.addActionListener(new ActionListener() {
					
					@SuppressWarnings("unchecked")
					@Override
					public void actionPerformed(ActionEvent arg0) {
						PedidoDTO pedidoDTO = new PedidoDTO();
						pedidoDTO.setIdCliente(Long.parseLong(idClienteTextField.getText()));
						for (Vector<String> row : 
							((Vector<Vector<String>>)itemPedidoModel.getDataVector())) {

							idcomidas.add(Long.valueOf(row.elementAt(0)));
							cantComidas.add(Integer.valueOf(row.elementAt(1)));
						}

						pedidoDTO.setCantComidas(cantComidas);
						pedidoDTO.setIdcomidas(idcomidas);
						precioTextField.setText((String.valueOf(Sistema.get().
								getPedidoControlador().obtenerPrecioPedido(pedidoDTO))));
					
						
					}
				});
			}
			{
				agregarItemPedidoButton = new JButton();
				agregarItemPedidoButton.setText("Agregar ItemPedido");
				agregarItemPedidoButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						itemPedidoModel.addRow(new String[]{"",""});
					}
				});
			}
			{
				aceptarButton = new JButton();
				aceptarButton.setText("Aceptar Pedido");
				aceptarButton.addActionListener(new ActionListener() {



					@SuppressWarnings("unchecked")
					public void actionPerformed(ActionEvent e) {
						final PedidoDTO pedidoDTO = new PedidoDTO();
						pedidoDTO.setIdCliente(Long.parseLong(idClienteTextField.getText()));
						pedidoDTO.setCantComidas(cantComidas);
						pedidoDTO.setIdcomidas(idcomidas);
						Sistema.get().getPedidoControlador().altaPedido(pedidoDTO);
					
					}
					
				});
			}


			{
				idClienteTextField = new JTextField();
			}
			{
				scroll = new JScrollPane();
				getContentPane().add(scroll);
				scroll.setBounds(23, 101, 354, 141);
				{
					this.itemPedidoModel = new DefaultTableModel();
					itemPedidoModel.addColumn("ID Comida");
					itemPedidoModel.addColumn("Cantidad");
					itemPedido = new JTable();
					scroll.setViewportView(itemPedido);
					itemPedido.setModel(itemPedidoModel);
				}
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(28, 28)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(idClienteTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(idClienteLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(precioLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(precioTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(50)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(scroll, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(16)
				        .addComponent(obtenerPrecioButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				        .addGap(102)))
				.addGap(17)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(agregarItemPedidoButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
				    .addComponent(aceptarButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(24, 24));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(23, 23)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(scroll, GroupLayout.PREFERRED_SIZE, 354, GroupLayout.PREFERRED_SIZE)
				        .addGap(30)
				        .addComponent(obtenerPrecioButton, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 12, Short.MAX_VALUE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(7)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(idClienteLabel, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
				                .addGap(31)
				                .addComponent(idClienteTextField, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
				                .addGap(50))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGap(38)
				                .addComponent(agregarItemPedidoButton, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)))
				        .addGap(35)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(precioLabel, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
				                .addGap(65)
				                .addComponent(precioTextField, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 19, Short.MAX_VALUE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addPreferredGap(precioLabel, aceptarButton, LayoutStyle.ComponentPlacement.INDENT)
				                .addComponent(aceptarButton, 0, 201, Short.MAX_VALUE)))))
				.addContainerGap(28, 28));

			pack();
			this.setSize(567, 350);
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}

}
